package com.har.modules.business.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.alibaba.fastjson.JSONObject;
import com.har.common.utils.PageUtils;
import com.har.common.utils.R;
import com.har.modules.business.entity.TRegisterEntity;
import com.har.modules.business.service.TRegisterService;
import com.har.utils.date.DateUtil;
import com.har.utils.security.Base64Util;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;



/**
 * 申请表
 *
 * @author macwy
 * @email 
 * @date 2018-06-26 16:38:41
 */
@RestController
@RequestMapping("business/tregister")
public class TRegisterController {
    @Autowired
    private TRegisterService tRegisterService;

    @Value("${config.downFolderPath}")
    private String downFolderPath;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:tregister:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tRegisterService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("business:tregister:info")
    public R info(@PathVariable("id") Long id){
        TRegisterEntity tRegister = tRegisterService.selectById(id);

        return R.ok().put("tRegister", tRegister);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:tregister:save")
    public R save(@RequestBody TRegisterEntity tRegister){
        tRegisterService.insert(tRegister);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:tregister:update")
    public R update(@RequestBody TRegisterEntity tRegister){
//        ValidatorUtils.validateEntity(tRegister);
        tRegisterService.updateAllColumnById(tRegister);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("business:tregister:delete")
    public R delete(@RequestBody Long[] ids){
        tRegisterService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }



    @RequestMapping("/export")
    @RequiresPermissions("business:tregister:export")
    public  R export(@RequestBody String param) throws Exception {

        JSONObject params = JSONObject.parseObject(param);

        List<TRegisterEntity> dataList= tRegisterService.export(params);

        File file = new File(downFolderPath);
        if(!file.exists()){
            file.mkdirs();
        }
        String tOrderFolderFilePath = downFolderPath+"/"+ DateUtil.convertDateToStr(new Date(),DateUtil.FORMATE_YYYYMMDDHHMMSS)+".xls";
        File file1 = new File(tOrderFolderFilePath);



        FileOutputStream outputStream = new FileOutputStream(file1);
        Workbook workbook= ExcelExportUtil.exportExcel(new ExportParams(), TRegisterEntity.class,dataList);

        workbook.write(outputStream);

        return R.ok().put("filePath", Base64Util.encodeBase64(tOrderFolderFilePath));
    }

}
